package leetcode.editor.cn;

import java.util.HashSet;
import java.util.Set;

public class _3_LongestSubstringWithoutRepeatingCharacters {
    public static void main(String[] args) {
        Solution solution = new _3_LongestSubstringWithoutRepeatingCharacters().new Solution();
        int i = solution.lengthOfLongestSubstring("aabaab!bb");
        System.out.println(i);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {
        public int lengthOfLongestSubstring(String s) {
            int res = 0, left = 0, right = 0;
            Set<Character> set = new HashSet<>();
            while (right < s.length()) {
                char c = s.charAt(right);
                right++;
                if (!set.contains(c)) {
                    // 如果 set 中没有 c
                    set.add(c);
                    res = Math.max(res, right - left);
                } else {
                    // 如果 set 中有 c
                    while (s.charAt(left) != c) {
                        set.remove(s.charAt(left));
                        left++;
                    }
                    left++;
                }
            }
            return res;
        }
    }
//leetcode submit region end(Prohibit modification and deletion)

}